﻿//1. Write a program that fills and prints a matrix of size (n, n) as shown below: (examples for n = 4)

using System;
using System.Collections.Generic;

class FillsMatrixCases
{
    static void Main()
    {
        int numberN = int.Parse(Console.ReadLine());
        int[,] matrix = new int[numberN, numberN];
        
        CaseOne(matrix);
        CaseTwo(matrix);
        CaseTree(matrix);
        CaseFour(matrix);
    }

    private static void Print(int[,] matrix)
    {
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                Console.Write("{0, 3}", matrix[row, col]);
            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }

    static void CaseOne(int[,] matrix)
    {
        int numberOne = 1;
        for (int col = 0; col < matrix.GetLength(1); col++)
        {
            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                matrix[row, col] = numberOne;
                numberOne += 1;
            }
        }
        Print(matrix);
    }

    static void CaseTwo(int[,] matrix)
    {
        int numberOne = 1;
        for (int col = 0; col < matrix.GetLength(1); col++)
        {
            if (col % 2 == 0)
            {
                for (int row = 0; row < matrix.GetLength(0); row++)
                {
                    matrix[row, col] = numberOne;
                    numberOne += 1;
                }
            }
            else
            {
                for (int row = matrix.GetLength(0) - 1; row >= 0; row--)
                {
                    matrix[row, col] = numberOne;
                    numberOne += 1;
                }
            }
        }

        Print(matrix);
    }

    static void CaseTree(int[,] matrix)
    {
        int numberOne = 1;
        for (int row = matrix.GetLength(0) - 1; row >= 0; row--)
        {
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                row += col;
                matrix[row, col] = numberOne;
                numberOne++;
                if (row == matrix.GetLength(0) - 1)
                {
                    row -= col;
                    break; 
                }
                row -= col;
            }
        }

        for (int col = 1; col < matrix.GetLength(0); col++)
        {
            for (int row = 0; row < matrix.GetLength(1); row++)
            {
                col += row;
                matrix[row, col] = numberOne;
                numberOne++;
                if (col == matrix.GetLength(0) - 1)
                {
                    col -= row;
                    break;
                }
                col -= row;
            }
        }

        Print(matrix);
    }
    static void CaseFour(int[,] matrix)
    {
        int numberOne = 1;
        int row = 0;
        int col = 0;
        for (int loop = 0; loop <= (matrix.GetLength(0) - 1) / 2; loop++)
        {
            for (row = row; row < matrix.GetLength(0) - loop; row++)
            {
                matrix[row, col] = numberOne;
                numberOne++;
            }
            row -= 1;
            for (col = col + 1; col < matrix.GetLength(1) - loop; col++)
            {
                matrix[row, col] = numberOne;
                numberOne++;
            }
            col -= 1;
            for (row = row - 1;  row > 0 - 1 + loop; row--)
            {
                matrix[row, col] = numberOne;
                numberOne++;
            }
            row += 1;
            for (col = col - 1; col > 0 + loop; col--)
            {
                matrix[row, col] = numberOne;
                numberOne++;
            }
            row++;
            col++;
        }

        Print(matrix);
    }
}
